<?php

namespace app\index\controller;
use app\BaseController;
use app\index\model\UserModel;
use app\index\service\Oss as OssService;
use OSS\Core\OssException;
use app\index\model\FileOss as FileOssModal;


class Oss extends BaseController
{
    protected $middleware = ['Auth'];
    /*
     * 上传文件
     */
    public function upload()
    {
        $file = request()->file('file');
        if (!$file) {
            return result()->fail('请选择要上传的文件', 400);
        }
        try {
            $ossInfo = new OssService();
            $info = $ossInfo->upload($file);
            $sizeSizeMb = $file->getSize() / 1024 / 1024;
            $params = [
                'userId' => $this->request->user->id,
                'delPath' => $info['path'],
                'fileSize' =>  $sizeSizeMb,
            ];
            $sql = new FileOssModal();
            $sql->save($params);
            $userFileSize = $this->request->user->fileSize;
            $userFileSize = $userFileSize + $sizeSizeMb;
            $userSql = new UserModel();
            $userSql->update([
                'fileSize' => $userFileSize
            ], ['id' => $this->request->user->id]);
            $path = $info['signedUrl'];
            $path = explode('?', $path);
            return result()->success([
                'url' => $path[0]
            ]);
        } catch (OssException $e) {
            return  result()->fail($e->getMessage(), 500);
        }
    }
  /*
   * 删除文件
   */
    public function delImg() {
        $id = $this->request->post('id');
        $sql = new FileOssModal();
        $info = $sql->where('id', $id)->findOrEmpty();
        if($info->isEmpty()) {
            return result()->fail('文件不存在', 400);
        }
        if($info->userId != $this->request->user->id) {
            return result()->fail('权限不足', 400);
        }
        try {
            $ossInfo = new OssService();
            $ossInfo->del($info->delPath);
            $sizeSizeMb = $info->fileSize;
            $userFileSize = $this->request->user->fileSize;
            $userFileSize = $userFileSize - $sizeSizeMb;
            $info->delete();
            $userSql = new UserModel();
            $userSql->update([
                'fileSize' => $userFileSize
            ], ['id' => $this->request->user->id]);
            return result()->success('删除成功');
        } catch (OssException $e) {
            return  result()->fail($e->getMessage(), 500);
        }
    }

    public function ossSignUrl() {
        $id = $this->request->post('id');
        $sql = new FileOssModal();
        $info = $sql->where('id', $id)->findOrEmpty();
        if($info->isEmpty()) {
            return result()->fail('文件不存在', 400);
        }
        try {
            $ossInfo = new OssService();
            $signedUrl = $ossInfo->sign($info->delPath);
            return result()->success([
                'url' => $signedUrl
            ]);
        } catch (OssException $e) {
            return  result()->fail($e->getMessage(), 500);
        }
    }
}